package com.example.base.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.base.system.domain.vo.SysDicCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.example.base.limit.IPRateLimit;
import java.util.List;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.example.base.config.PageData;
import com.example.base.system.service.SysDicCategoryService;
import com.example.base.system.domain.SysDicCategory;
import java.io.Serializable;

/**
 * <p>
 * 字典类别 前端控制器
 * </p>
 *
 * @author xiong
 * @since 2024-02-19
 */

@RestController
@Api(tags = {"字典类别的服务类"})
@RequestMapping("/sys-dic-category")
public class SysDicCategoryController  {

        @Autowired
        private SysDicCategoryService sysDicCategoryService;


        @PostMapping("/saveorupdate")
        @IPRateLimit(name = "SysDicCategory-save",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "字典类别的新增接口",notes = "字典类别的新增接口")
        public JSONObject save(@RequestBody @Validated SysDicCategoryVo sysDicCategory) throws GlobalExcetion{
                 sysDicCategoryService.saveOrUpdate(sysDicCategory);
                 return Result.successInfo("保存成功");
        }


        @PostMapping("/update")
        @IPRateLimit(name = "SysDicCategory-update",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "字典类别的更新接口",notes = "字典类别的更新接口")
        public JSONObject update(@RequestBody SysDicCategory sysDicCategory) throws GlobalExcetion{
                sysDicCategoryService.updateSysDicCategoryById(sysDicCategory);
                return Result.successInfo("更新成功");
        }



        @DeleteMapping("/{id}")
        @IPRateLimit(name = "SysDicCategory-del",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "字典类别的删除接口",notes = "字典类别的删除接口")
        public JSONObject delete(@PathVariable("id") Serializable id) throws GlobalExcetion{
                boolean flag = sysDicCategoryService.removeById(id);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @PostMapping("/del/batch")
        @IPRateLimit(name = "SysDicCategory-delbatch",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "字典类别的批量删除接口",notes = "字典类别的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids) throws GlobalExcetion {
                boolean flag =  sysDicCategoryService.removeByIds(ids);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @GetMapping("/getall")
        @IPRateLimit(name = "SysDicCategory-getall",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "字典类别的获取全部接口",notes = "字典类别的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
                List<SysDicCategory> list= sysDicCategoryService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/{id}")
        @IPRateLimit(name = "SysDicCategory-getid",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "字典类别的获取一个接口",notes = "字典类别的获取一个接口")
        public JSONObject findOne(@PathVariable("id") Serializable id) throws GlobalExcetion {
                SysDicCategory data= sysDicCategoryService.getById(id);
                return Result.successInfo(data);
        }

        @GetMapping("/page")
        @IPRateLimit(name = "SysDicCategory-page",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "字典类别的分页接口",notes = "字典类别的分页接口")
        public JSONObject findPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) throws GlobalExcetion {
                QueryWrapper<SysDicCategory> queryWrapper = new QueryWrapper<>();
                IPage<SysDicCategory> data=sysDicCategoryService.page(new Page<>(page, pageSize),queryWrapper);
                PageData pageData=new PageData(data.getTotal(),page,pageSize,data.getRecords());
                return Result.successInfo(pageData);
        }

        @GetMapping("/list")
        @ApiOperation(value = "字典类别列表接口",notes = "字典类别列表接口")
        public JSONObject list(){
                List<SysDicCategory> list=sysDicCategoryService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/treelist")
        @ApiOperation(value = "字典类别树列表接口",notes = "字典类别列表接口")
        public JSONObject treeList(){
                List<SysDicCategory> treeList=sysDicCategoryService.treeList();
                return Result.successInfo(treeList);
        }


}

